<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="资源分类名称" prop="resourceClassifyName">
        <el-input
          v-model="queryParams.resourceClassifyName"
          placeholder="请输入资源分类名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资源名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入资源名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="别名" prop="alias">
        <el-input
          v-model="queryParams.alias"
          placeholder="请输入别名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否测试" prop="testStatus">
        <el-select
          v-model="queryParams.testStatus"
          placeholder="请选择是否测试"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="轮播展示" prop="home">
        <el-select
          v-model="queryParams.home"
          placeholder="请选择轮播展示"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上架状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择上架状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions('cms_resource_status')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:resource-dir:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button @click="handleDeleteList" type="danger"> 批量删除</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:resource-dir:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="别名" align="center" prop="alias" />
      <el-table-column label="类型" align="center" prop="resourceClassifyName" />
      <el-table-column label="描述" align="center" prop="remark" />
      <el-table-column label="页面截图" align="center" prop="picUrl" width="110">
        <template #default="scope">
          <el-image
            :src="scope.row.picUrl"
            :preview-src-list="scope.row.picUrl"
            fit="cover"
            class="h-80px w-80px"
            lazy
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="视频介绍" align="center" prop="videoUrl" width="180">
        <template #default="scope">
          <VideoPlayer
            :url="scope.row.videoUrl"
          />
        </template>
      </el-table-column>
      <el-table-column label="支持系统" align="center" prop="supportSystemList">
        <template #default="scope">
          <dict-tag :type="'cms_support_system'" :value="scope.row.supportSystemList" />
        </template>
      </el-table-column>
      <el-table-column label="其他下載地址" align="center" prop="otherDownloadUrl" />
      <el-table-column label="激活码购买地址" align="center" prop="activityUrl" />
      <el-table-column label="优惠码" align="center" prop="activityCode" />
      <el-table-column label="是否测试" align="center" prop="testStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.testStatus" />
        </template>
      </el-table-column>
      <el-table-column label="小提示" align="center" prop="tips" />
      <el-table-column label="轮播展示" align="center" prop="home">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.home" />
        </template>
      </el-table-column>
      <el-table-column label="相关分类" align="center" prop="detail" />
      <el-table-column label="上架状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="'cms_resource_status'" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:resource-dir:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:resource-dir:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ResourceDirForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ResourceDirApi, ResourceDirVO } from '@/api/system/resourcedir'
import ResourceDirForm from './ResourceDirForm.vue'
import { ResourceApi } from '@/api/system/resource'

/** 资源命名 列表 */
defineOptions({ name: 'ResourceDir' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ResourceDirVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  resourceClassifyName: undefined,
  name: undefined,
  alias: undefined,
  testStatus: undefined,
  home: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const multipleSelection = ref([])

const handleSelectionChange = (val: []) => {
  const ids = val.map((item) => item.id)
  console.log(val, 'val，ids', ids)
  multipleSelection.value = ids
}

/** 删除按钮操作 */
const handleDeleteList = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ResourceDirApi.deleteResourceDirs(multipleSelection.value)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ResourceDirApi.getResourceDirPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ResourceDirApi.deleteResourceDir(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ResourceDirApi.exportResourceDir(queryParams)
    download.excel(data, '资源命名.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
